package com.augus.admin.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.zaxxer.hikari.util.DriverDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;

@Deprecated // 过时注解
/*数据源文件*/
//@Configuration // 注销之后 这个文件的东西就不进入配置,pox 有优化的
public class MyDataSourceConfig {

  // 默认的自动配置是判断容器中没有才会配@ConditionalOnMissingBean(DataSource.class)
  // 自己创建的数据源
  // 默认的自动配置
  @ConfigurationProperties("spring.datasource")

//  @Bean
  public DataSource dataSource() throws SQLException {

    DruidDataSource druidDataSource = new DruidDataSource();
    /* druidDataSource.setUrl();
    druidDataSource.setUsername();
    druidDataSource.setPassword();
*/
    // 也可以在配置文件yml写
    // 加入监控功能
    // stat 监控
    // wall 防火墙
//    druidDataSource.setFilters("stat,wall");

//    druidDataSource.setMaxActive(10);
    return druidDataSource;
  }

  /**
   * 配置 druid的监控页功能
   */
//  @Bean
  public ServletRegistrationBean statViewServlet() {
    StatViewServlet statViewServlet = new StatViewServlet();
    ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<StatViewServlet>(statViewServlet, "/druid/*");

    // 配置初始化参数登录 用户名和密码
    registrationBean.addInitParameter("loginUsername", "admin");
    registrationBean.addInitParameter("loginPassword", "123456");
    return registrationBean;
  }

  /**
   * webstatFilter用于采集web-jdbc关联监控的数据。
   */

  // 监控WEB应用
//  @Bean
  public FilterRegistrationBean webStatFilter() {
    WebStatFilter webStatFilter = new WebStatFilter();

    FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<>(webStatFilter);
    filterRegistrationBean.setUrlPatterns(Arrays.asList("/*")); // 拦截所有的路径
    filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");// 排除拦截路径
    return filterRegistrationBean;
  }
}
